package main

import (
	"encoding/json"
	"fmt"
	"log"
	"time"

	sls "github.com/aliyun/aliyun-log-go-sdk"
)

func main() {
	Endpoint := "cn-hongkong.log.aliyuncs.com"
	AccessKeyId := "LTAI5tGjJnh66c1txANiRBQN"
	AccessKeySecret := "IZa8HrpmQh7xzd0TIEFQe8YpRX8zOh"
	SecurityToken := ""
	ProjectName := "wxhk-backend"
	LogStoreName := "gw-kong-log"
	topic := ""
	var offset int64 = 0
	var pageSize int64 = 100
	endAt := time.Now().Unix()
	startAt := endAt - 3600*24*30

	client := sls.CreateNormalInterface(Endpoint, AccessKeyId, AccessKeySecret, SecurityToken)

	items := []string{}
	for _, item := range items {
		response, err := client.GetLogs(ProjectName, LogStoreName, topic, startAt, endAt, item, pageSize, offset, true)
		if err != nil {
			log.Fatalf("GetLogs failed %v", err)
		}

		if response.Count > 0 {
			var request Request
			err = json.Unmarshal([]byte(response.Logs[0]["raw"]), &request)
			if err != nil {
				panic(err)
			}
			fmt.Println(request.ClientIp)
		} else {
			fmt.Println("")
		}
	}
}

type Request struct {
	StartedAt int64  `json:"started_at"`
	ClientIp  string `json:"client_ip"`
	// Request string
	Status uint
	// Response string
	Hostname    string
	UpstreamUri string `json:"upstream_uri"`
	// Service string
	// Route string
	// Tries string
	// Latencies string
}

// type Student struct {
// 	Name string
// 	Age int
// 	Skill string
// }

// stu := Student{"tom", 12, "football"}
// data, err := json.Marshal(&stu)
// if err != nil {
// 	panic(err)
// }
// fmt.Println(string(data))

// str := `{"Name":"tom","Age":12,"Skill":"football"}`
// var stu2 Student
// err := json.Unmarshal([]byte(str), &stu2)
// if err != nil {
// 	panic(err)
// }
// fmt.Printf(stu2)
